using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Design;

namespace Library.WebControls.SimpleButton {
	///<summary>
	/// Class that provide the button functionality
	///</summary>
	[ToolboxBitmap(typeof(SpButton), "Library.WebControls.SimpleButton.SpButton.bmp")]
	[Designer(typeof(SpButtonDesign))]
	[SupportsEventValidation]
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:SpButton runat=server></{0}:SpButton>")]
	[DesignTimeVisible(true)]

	public class SpButton : BaseControl {
		/// <summary>
		/// Creates a new SpButton object
		/// </summary>
		public SpButton() {
			ClientScript = new ClientScript();
		}
		protected override string StyleSheetFileName {
			get { return "spbutton-ui.css"; }
		}

		protected internal string DisableCssClass = "spDisableButton";
		protected internal string EnableClass = "spButton";
		protected internal string ImgCssClass = "spImageButton";
		protected internal string TextCssClass = "spTextButton";
		protected internal string MouseDownCssClass = "spButtonMouseDown";
		protected internal string MouseUpCssClass = "spButtonMouseUp";
		protected internal string PaddingClass = "spBtnPadding";

		private System.Web.UI.WebControls.Image _img;
		private HtmlGenericControl _divButton;
		private HtmlGenericControl _divImage;
		private HtmlGenericControl _divText;
		protected override void CreateChildControls() {
			_img = new System.Web.UI.WebControls.Image();
			_divButton = new HtmlGenericControl("div");
			var paddiing = new HtmlGenericControl("div");
			paddiing.Attributes["class"] = PaddingClass;
			_divButton.Controls.Add(paddiing);

			_divImage = new HtmlGenericControl("div");
			_divImage.Controls.Add(_img);
			_divImage.Visible = false;
			_divText = new HtmlGenericControl("div");
			paddiing.Controls.Add(_divImage);
			paddiing.Controls.Add(_divText);
			_divText.Attributes["class"] = TextCssClass;
			_divImage.Attributes["class"] = ImgCssClass;
			Controls.Add(_divButton);
		}

		internal HtmlGenericControl DivButton {
			get {
				EnsureChildControls();
				return _divButton;
			}
		}
		/// <summary>
		/// Button width
		/// </summary>
		public Unit Width {
			get {
				EnsureChildControls();
				return new Unit(_divButton.Style[HtmlTextWriterStyle.Width]);
			}
			set {
				EnsureChildControls();

				_divButton.Style[HtmlTextWriterStyle.Width] = value.ToString();
			}
		}
		/// <summary>
		/// Button text
		/// </summary>
		public string Text {
			get {
				EnsureChildControls();
				return _divText.InnerText;
			}
			set {
				EnsureChildControls();
				_divText.InnerText = value;
			}
		}
		/// <summary>
		/// Image url, image display at the left of button text
		/// </summary>
		[DefaultValue("")]
		[Editor(
			"System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
			, typeof(UITypeEditor))]
		[UrlProperty]
		[Bindable(true)]

		public string ImageUrl {
			get { EnsureChildControls(); return _img.ImageUrl; }
			set {
				EnsureChildControls();
				_img.ImageUrl = value;
				_divImage.Visible = !String.IsNullOrEmpty(value);
			}
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			IncludeScriptOnLoad(typeof(SpButton), "SpButton", "Library.WebControls.SimpleButton.include.SpButton.js");
		}


		//public string ClickFunction { get; set; }

		public override string ID {
			get {
				EnsureChildControls();
				return _divButton.ID;
			}
			set {
				EnsureChildControls();
				_divButton.ID = value;
			}
		}

		public override string ClientID {
			get {
				EnsureChildControls();
				return _divButton.ClientID;
			}
		}

		protected override void CreateClientObjectAtRender(HtmlTextWriter writer) {
			BeginScriptTag(writer);
			writer.Write("var {0}=new SpButton('{0}','{1}',{2},'{3}','{4}','{5}','{6}');",
				ClientName,
				ClientID,
				ClientEnable.ToString().ToLower(),
				MouseDownCssClass,
				MouseUpCssClass,
				EnableClass, DisableCssClass);

			AddScriptHandler(writer, 1, ClientScript.Click, "AddHandler");

			EndScriptTag(writer);
		}
		/// <summary>
		/// Client script contains some function can hook to at the client
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		[MergableProperty(false)]
		[Category("Data")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[DefaultValue("")]
		public ClientScript ClientScript { private set; get; }
		protected override void Render(HtmlTextWriter writer) {
			if (!ClientVisible)
				_divButton.Style.Add(HtmlTextWriterStyle.Display, "none");

			if (ClientEnable)
				_divButton.Attributes["class"] = EnableClass;
			else
				_divButton.Attributes["class"] = DisableCssClass;


			_divButton.Attributes["onclick"] = ClientName + ".Call(1)";
			_divButton.Attributes["onmousedown"] = ClientName + ".Call(2)";
			_divButton.Attributes["onmouseup"] = ClientName + ".Call(3)";


			base.Render(writer);
		}
	}
}
